package com.xunyin.modules.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.common.utils.QueryHelpPlus;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.area.domain.TAreaSpace;
import com.xunyin.modules.area.service.TAreaSpaceService;
import com.xunyin.modules.store.domain.TStore;
import com.xunyin.modules.store.param.TStoreQueryParam;
import com.xunyin.modules.store.service.TStoreService;
import com.xunyin.modules.store.service.dto.TStoreDto;
import com.xunyin.modules.store.service.dto.TStoreQueryCriteria;
import com.xunyin.modules.store.service.mapper.TStoreMapper;
import com.xunyin.modules.store.vo.TStoreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @date 2022-09-13
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class TStoreServiceImpl extends BaseServiceImpl<TStoreMapper, TStore> implements TStoreService {

    @Autowired
    private IGenerator generator;

    @Autowired
    private TAreaSpaceService tAreaSpaceService;

    /**
     * 查询数据分页
     *
     * @param criteria 条件
     * @param pageable 分页参数
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> queryAll(TStoreQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<TStore> page = new PageInfo<>(queryAll(criteria));
        List<TStoreDto> tStoreDTOS = new ArrayList<>();
        for (TStore tStore : page.getList()) {
            this.tStoreList(tStoreDTOS, tStore);
        }
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", tStoreDTOS);
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<TStore> queryAll(TStoreQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(TStore.class, criteria));
    }

    /**
     * 转换门店的省、市、县名称
     *
     * @param tStoreDTOS 门店列表
     * @param tStore     门店
     */
    private void tStoreList(List<TStoreDto> tStoreDTOS, TStore tStore) {
        TStoreDto tStoreDto = generator.convert(tStore, TStoreDto.class);

        // 设置省名称
        TAreaSpace tAreaSpaceProvince = tAreaSpaceService.getTAreaSpaceByAreaCode(tStore.getProvinceCode());
        tStoreDto.setProvinceName(tAreaSpaceProvince.getAreaName());

        // 设置市名称
        TAreaSpace tAreaSpaceCity = tAreaSpaceService.getTAreaSpaceByAreaCode(tStore.getCityCode());
        tStoreDto.setCityName(tAreaSpaceCity.getAreaName());

        // 设置县名称
        TAreaSpace tAreaSpaceCounty = tAreaSpaceService.getTAreaSpaceByAreaCode(tStore.getCountyCode());
        tStoreDto.setCountyName(tAreaSpaceCounty.getAreaName());
        tStoreDTOS.add(tStoreDto);
    }

    /**
     * 获取门店列表
     *
     * @return List<TStoreVo>
     */
    @Override
    public List<TStoreVo> listStore(TStoreQueryParam queryParam) {
        LambdaQueryWrapper<TStore> wrapper = new LambdaQueryWrapper<>();
        if (queryParam.getMainId() != null) {
            wrapper.eq(TStore::getMainId, queryParam.getMainId());
        }
        List<TStoreVo> list = generator.convert(baseMapper.selectList(wrapper), TStoreVo.class);
        return list;
    }

    /**
     * 根据管理员手机号码获取门店信息
     *
     * @param queryParam
     * @return
     */
    @Override
    public TStore getInfoTeaByManagePhone(TStoreQueryParam queryParam) {
        LambdaQueryWrapper<TStore> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(TStore::getRemark, queryParam.getManagePhone());
        TStore tStore = baseMapper.selectOne(wrapper);
        return tStore;
    }

    /**
     * 查询会员卡使用网点
     *
     * @return List<TStoreVo>
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<TStoreVo> getTeaHouseArray(String teaHouseIdArray) {
        LambdaQueryWrapper<TStore> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TStore::getMainId, teaHouseIdArray);
        List<TStoreVo> list = generator.convert(baseMapper.selectList(wrapper), TStoreVo.class);
        return list;
    }
}
